package com.test.base.service;

import com.test.base.dao.NideshopRegionDao;
import com.test.base.entity.NideshopRegion;
import com.test.base.utils.Assist;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RegionService {
    @Autowired
    private NideshopRegionDao nideshopRegionDao;

    /**
     * 获取完整的省市区名称组成的字符串
     *
     * @param provinceId
     * @param cityId
     * @param districtId
     * @returns
     */
    public String getFullRegionName(Long provinceId, Long cityId, Long districtId) {
        boolean checkBool = checkFullRegion(provinceId, cityId, districtId);
        if (!checkBool) {
            return "";
        } else {
            StringBuilder sbr = new StringBuilder();
            Assist region = new Assist();
            region.setRequires(Assist.customRequire("in id = (", provinceId + "," + cityId + "," + districtId, ")"));
            region.setOrder("id", true);
            region.setStartRow(0);
            region.setRowSize(3);
            List<NideshopRegion> regionlList = nideshopRegionDao.selectNideshopRegion(region);
            for (NideshopRegion nr : regionlList) {
                sbr.append(nr.getName());
            }
            return sbr.toString();
        }
    }

    /**
     * 检查省市区信息是否完整和正确
     *
     * @param provinceId
     * @param cityId
     * @param districtId
     * @returns
     */
    public boolean checkFullRegion(Long provinceId, Long cityId, Long districtId) {
        if (null == provinceId || null == cityId || null == districtId)
            return false;
        Assist region = new Assist();
        region.setRequires(Assist.customRequire("in id = (", provinceId + "," + cityId + "," + districtId, ")"));
        region.setOrder("id", true);
        region.setStartRow(0);
        region.setRowSize(3);
        List<NideshopRegion> regionlList = nideshopRegionDao.selectNideshopRegion(region);
        if (regionlList.isEmpty() || regionlList.size() != 3)
            return false;
        if (regionlList.get(0).getId() != regionlList.get(1).getParentId()) {
            return false;
        }
        if (regionlList.get(1).getId() != regionlList.get(2).getParentId()) {
            return false;
        }
        return true;
    }

    /**
     * 获取区域的名称
     *
     * @param regionId
     * @returns
     */
    public String getRegionName(Integer regionId) {
        NideshopRegion region = nideshopRegionDao.selectNideshopRegionById(regionId);
        return region.getName();
    }


    /**
     * 获取下级的地区列表
     *
     * @param parentId
     * @returns
     */
    public List<NideshopRegion> getRegionList(Long parentId) {
        Assist region = new Assist();
        region.setRequires(Assist.andEq("parent_id", parentId));
        return nideshopRegionDao.selectNideshopRegion(region);
    }

    /**
     * 获取区域的信息
     *
     * @param regionId
     * @returns
     */
    public NideshopRegion getRegionInfo(Long regionId) {
        return nideshopRegionDao.selectNideshopRegionById(regionId);
    }
}
